package ru.ys.jsolver.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.JComponent;

import ru.ys.jsolver.interfaces.IBoard;

@SuppressWarnings("serial")
public class BoardComponent extends JComponent {	
	private int myCellWidth = 20;
	private int myBorderWidth = 3;
	
	private int getCC(int x) {
		return ((myCellWidth + myBorderWidth) * x);
	}
	
	
	final IBoard myBoard;
	
	public BoardComponent(IBoard board) {
		super();
		myBoard = board;
	}
	
	@Override
	public Dimension getMinimumSize() {		
		return getPreferredSize();		
	}
	
	@Override
	public Dimension getPreferredSize() {		
		return new Dimension(myBoard.getWidth() * (myBorderWidth + myCellWidth),
							myBoard.getHeight() * (myBorderWidth + myCellWidth));				
	}
	
	@Override
	public Dimension getMaximumSize() {
		return getPreferredSize();		
	}


	public void paintComponent(Graphics g) {
		// drawing board
		for (int i = 0; i < myBoard.getWidth(); i++) {
			for (int j = 0; j < myBoard.getHeight(); j++) {
				switch (myBoard.getPointState(i, j)) {
				case EMPTY:
					g.setColor(Color.WHITE);
					g.fillRect(getCC(i), getCC(j), myCellWidth, myCellWidth);
					int fraction = 8;
					g.setColor(Color.BLACK);
					g.fillOval(getCC(i) + myCellWidth / 2 - myCellWidth / fraction,
								getCC(j) + myCellWidth / 2 - myCellWidth / fraction,
								myCellWidth / fraction * 2, myCellWidth / fraction * 2);
					break;
				case HYPOTHESIS:
					g.setColor(Color.YELLOW);
					g.fillRect(getCC(i), getCC(j), myCellWidth, myCellWidth);					
					break;
				case SET:
					g.setColor(Color.BLACK);
					g.fillRect(getCC(i), getCC(j), myCellWidth, myCellWidth);					
					break;					
				case UNKNOWN:
					g.setColor(Color.WHITE);
					g.fillRect(getCC(i), getCC(j), myCellWidth, myCellWidth);
					break;
				}
			}
		}		
	}
}
